package edu.hawaii.duedates.lender;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.List;
import org.junit.Test;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebResponse;
import edu.hawaii.duedates.ItemDue;

/**
 * Tests the functionality of the UhManoaLibrary class that implements
 * the libraryVault class.
 * 
 * @author Tyler Wolff and Scheller Sanchez
 */
public class TestUhManoaLibrary {

  private final boolean useMockLibraries = ("true".equals(System.getProperty("useMockLibraries")));
  
  // The user's UH ID number.
  private final String uhTestId1 = System.getenv("DUEDATES_TESTUSER1_ID");
  private final String uhTestId2 = System.getenv("DUEDATES_TESTUSER2_ID");

  // The user's last name.
  private final String lastTestName1 = System.getenv("DUEDATES_TESTUSER1_LASTNAME");
  private final String lastTestName2 = System.getenv("DUEDATES_TESTUSER2_LASTNAME");
  
  /**
   * Testing basic object creation within class.
   */
  @Test
  public void testUhManoaLibrary() {
    UhManoaLibrary uhManoa = new UhManoaLibrary();
    assertEquals("testing library name", "UH Manoa Library", uhManoa.getLenderName());
    assertNotNull("library1 not found", uhManoa);
  }
  
  /**
   * Tests uh login for the login method. Tests correct login information.
   * 
   * @exception Exception if login errors.
   */
  @Test
  public void testLoginUh() throws Exception {
    
    UhManoaLibrary uhManoa = new UhManoaLibrary();
    if (this.useMockLibraries) {
      assertNotNull("Skipping testLoginUh", uhManoa);
    }
    else {
      WebConversation conversation = uhManoa.login(this.uhTestId2, this.lastTestName2);
      WebResponse response = conversation.getCurrentPage();
      assertNotNull("Testing correct Login", conversation);
      assertTrue("Testing correct Login Display", response.getText().contains("Checked Out Items"));
    }
  }
  
  /**
   * Tests incorrect login to the UH Manoa library.
   * 
   * @throws Exception if login errors.
   * @exception Exception if login errors.
   */
  @Test
  public void testInvalidLoginUh() throws Exception {
    
    UhManoaLibrary uhManoatwo = new UhManoaLibrary();
    if (this.useMockLibraries) {
      assertNotNull("Skipping testInvalidLoginUh", uhManoatwo);
    }
    else {
      WebConversation conversation = uhManoatwo.login(this.uhTestId2 + "99", this.lastTestName2);
      assertNull("Testing incorrect Login", conversation);
    }
  }
  
  /**
   * Tests the getItemInfo method of the UhManoaLibrary class for
   * no items out.
   * @throws Exception if error occurs logging in.
   * 
   */
  @Test
  public void testNoItemsOut() throws Exception {   
    UhManoaLibrary uhManoa = new UhManoaLibrary();
    if (this.useMockLibraries) {
      assertNotNull("Skipping testNoItemsOut", uhManoa);
    }
    else {
      List<ItemDue> uhInfo = uhManoa.getItemInfo (this.uhTestId1, this.lastTestName1);  
      assertTrue("Testing no items currently checked out.", uhInfo.isEmpty());
    }
  }
  
  /**
   * Tests the getItemInfo method of the UhManoaLibrary class for
   * items out.
   * 
   * @throws Exception if error occurs logging in.
   */
  @Test
  public void testItemsOut() throws Exception {
    
    UhManoaLibrary uhManoa = new UhManoaLibrary();
    if (this.useMockLibraries) {
      assertNotNull("Skipping testItemsOut", uhManoa);
    }
    else {
      List<ItemDue> uhInfo = uhManoa.getItemInfo (this.uhTestId2, this.lastTestName2);
      assertEquals("Testing number of books out", 2, uhInfo.size());
    }
    
  }
  
}
